package com.gim.sap.business;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.telephony.TelephonyManager;

import com.gim.sap.database.DBHelper;
import com.gim.sap.database.MyDataBase;
import com.gim.sap.entity.SIMEntity;

public class SIMBusiness {

	// doinsert backup phone and pwd

	public void doInsertBackPhone(Context context, SIMEntity simEntity) {
		ContentValues values = new ContentValues();
		values.put("PhoneNumber", simEntity.getPhoneNumber());
		values.put("PhonePWD", simEntity.getPwd());
		values.put("PhoneSIMCode", simEntity.getPhoneCode());
		DBHelper.doInsert(context, MyDataBase.PHONETABLE, values);
	}

	// get the phone code

	public String getPhoneCode(TelephonyManager tlm) {
		// TelephonyManager tlm=(TelephonyManager)
		// getSystemService(Context.TELEPHONY_SERVICE);
		tlm.getSimSerialNumber();
		return tlm.getSimSerialNumber();
	}

	public String getPhoneCodeForDB(Context context) {
		String sql = "select distinct PhoneSIMCode from "
				+ MyDataBase.PHONETABLE;
		Cursor cursor = DBHelper.doSelect(sql, context);
		String code = "";
		if (cursor.moveToFirst()) {
			code = cursor.getString(0);
		}
		DBHelper.doClose(cursor);
		return code;
	}

	public void doInsertDialog(Context context, String telNumber) {
		ContentValues values = new ContentValues();
		values.put("tell", telNumber);
		DBHelper.doInsert(context, MyDataBase.DIALOG_TABLE, values);
	}

	public List<String> converCursorToList(Context context) {
		String sqlQuery = "select * from " + MyDataBase.DIALOG_TABLE;
		List<String> tells = new ArrayList<String>();
		Cursor cursor = DBHelper.doSelect(sqlQuery, context);
		while (cursor.moveToNext()) {
			String tell = cursor.getString(1);
			tells.add(tell);
		}
		DBHelper.doClose(cursor);
		return tells;
	}
}
